import Config from '../config/index'
const app_type : string = 'h5';
const app_type_name : string = 'H5';

export function request(url : string, params : any, method : any = 'POST') {
	let data = {
		app_type,
		app_type_name,
		...params
	}
	let header = {
		Authorization: uni.getStorageSync('token'),
		'content-type': 'application/json;charset=UTF-8'
	}

	return new Promise((resolve, reject) => {
		if (url.indexOf('/login') == -1) {
			const token = uni.getStorageSync('token')
			if (token && token.length > 0) {
				header.Authorization = token;
			} else if (params.uncheck) {

			}
		}
		if (url.indexOf('/wsapi') == -1) {
			header['language'] = uni.getLocale();
		}
		if (data) {
			if (data.id == 0) {
				data.id = ''
			}
			if (data.userId && data.userId == 0) {
				data.userId = ''
			}
		}
		uni.request({
			url: Config.baseUrl + url,
			method: method,
			data,
			header: header,
			dataType: 'json',
			responseType: 'text',
			success: (res : any) => {
				if (res.data.code != 200) {
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: 'error'
					})
				}
				if (res.data.code == 1002) {
					uni.removeStorageSync('token');
					uni.removeStorage({
						key: 'token'
					})
					reject(res.data);
					return
				}
				if (res.data.code == 1003) {
					// uni.removeStorageSync('token');
					// uni.removeStorage({
					// 	key:'token'
					// })
					reject(res.data);
					return
				}
				resolve(res.data);
			},
			fail: (res : any) => {
				reject(res);
			},
			complete: (res : any) => {
				//reject(res);
			}
		});
	});
}
export function uploadFile(url : string, params : any, files : any) {
	let data = {
		app_type,
		app_type_name,
		...params
	}
	let header = {
		'Authorization': '',
		'content-type': 'application/json;charset=UTF-8'
	}
	if (uni.getStorageSync('token')) {
		header.Authorization = uni.getStorageSync('token');
	}
	return new Promise((resolve, reject) => {
		const uploadTask = uni.uploadFile({
			url: Config.baseUrl + url,
			filePath: files,
			name: 'files',
			header: header,
			formData: data,
			success: res => {
				resolve(JSON.parse(res.data));
			},
			fail: (res : any) => {
				reject(res);
			},
			complete: (res : any) => {
				reject(res);
			}
		});
		uploadTask.onProgressUpdate((res) => {
			console.log('上传进度' + res.progress);
			console.log('已经上传的数据长度' + res.totalBytesSent);
			console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
			// 测试条件，取消上传任务。
			/* if (res.progress > 50) {
				uploadTask.abort();
			} */
		});
	});
}
export function uploadSingleFile(url : string, params : any, file : any) {
	let data = {
		app_type,
		app_type_name,
		...params
	}
	let header = {
		'Authorization': '',
		'content-type': 'application/json;charset=UTF-8'
	}
	if (uni.getStorageSync('token')) {
		header.Authorization = uni.getStorageSync('token');
	}
	return new Promise((resolve, reject) => {
		const uploadTask = uni.uploadFile({
			url: Config.baseUrl + url,
			filePath: file,
			name: 'file',
			header: header,
			formData: data,
			success: res => {
				resolve(JSON.parse(res.data));
			},
			fail: (res : any) => {
				reject(res);
			},
			complete: (res : any) => {
				reject(res);
			}
		});
		uploadTask.onProgressUpdate((res) => {
			console.log('上传进度' + res.progress);
			console.log('已经上传的数据长度' + res.totalBytesSent);
			console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
			// 测试条件，取消上传任务。
			/* if (res.progress > 50) {
				uploadTask.abort();
			} */
		});
	});
}
export default {
	post: (url : string, params : any) => {
		return request(url, params, 'POST')
	},
	get: (url : string, params : any) => {
		return request(url, params, 'GET')
	},
	delete: (url : string, params : any) => {
		return request(url, params, 'DELETE')
	},
	put: (url : string, params : any) => {
		return request(url, params, 'PUT')
	}
}